/*
 * The VPE Project
 *
 * ide/main.cpp
 *
 * Copyright (C) 2009 - Eneias Ramos de Melo.
 */

#include <QForms.hpp>
#include <stdio.h>
#pragma hdrstop

#include "AppProcs.h"
#include "Support.h"
USEFORM("uMainWindow.cpp", MainWindow);
USEFORM("uProjectOptions.cpp", ProjectOptions);
//---------------------------------------------------------------------------
#pragma package (smart_init)
#pragma argused


/* data */
char *prog_name;
bool no_splash, be_verbose;
static int vpe_argc;
static char **vpe_argv;


int
main (int argc, char **argv)
{
    bool show_help;
    int i, n_args;
    
    prog_name = argv[0];
    show_help = false;
    
    /* Parse argumments */
    n_args = argc;
    for (i = 1; i < argc; i++)
    {
	if (!strcmp (argv[i], "--help") ||
	    !strcmp (argv[i], "-h" ))
	{
	    show_help = true;
	}
	else if (!strcmp (argv[i], "--no-splash"))
	{
	    no_splash = true;
	    argv[i] = NULL;
	    n_args--;
	}
	else if (!strcmp (argv[i], "--verbose"))
	{
	    be_verbose = true;
	    argv[i] = NULL;
	    n_args--;
	}
	/*
	 * ANYTHING ELSE starting with a '-' is an error.
         */
	else if (argv[i][0] == '-')
	{
	    printf ("%s: %s\n", _("Invalid option"), argv[i]);
	    show_help = true;
	}
    }
    
    if (show_help) {
	printf ("%s: %s %s\n", _("Usage"), prog_name, _("[option ...] [files ...]"));
	printf ("%s\n%s\n%s\n%s\n%s\n",
		_( "Valid options are:"),
		_( "  -h --help                   Output this help."),
		_( "  --verbose                   Show startup messages."),
		_( "  --no-splash                 Do not show startup window."),
		_( "  -display <display>         Use the designated X display."));
	exit (255);
    }
    
    try 
    {
	Application->Initialize ();
	Application->Style->DefaultStyle = 0;
	Application->Title = "Visual Program Editor";
	
	/* Keep the command line arguments. For use vpe_init */
	vpe_argc = argc - 1;
	vpe_argv = argv + 1;
	
	vpe_init (vpe_argc, vpe_argv);
	
	Application->Run ();
	
	vpe_exit ();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
	}
	catch(Exception &exception)
	{
	    Application->ShowException(&exception);
	}
    }
    
    return 0;
}
